package thread;
/**
 * 线程并发安全问题
 */
public class SyncDemo1 {
    public static void main(String[] args) {
        Shop shop1 = new Shop();
        Shop shop2 = new Shop();
        /*
        Thread t1 = new Thread("王克晶"){
            public void run(){
                shop1.buy();
            }
        };
        Thread t2 = new Thread("范传奇"){
            public void run(){
                shop2.buy();
            }
        };
        */
        Thread t1 = new Thread(shop1::buy);
        Thread t2 = new Thread(shop2::buy);
        t1.start();
        t2.start();
    }
}
class Shop {
    //public synchronized void buy(){ //在方法上同步监视（加锁），范围比较大
    public void buy(){
        Thread t = Thread.currentThread();
        try {
            System.out.println(t.getName()+"正在挑衣服");
            Thread.sleep(3000);
            //synchronized (this) { //同步监视块，缩小了监视范围
            //synchronized (new Object()) { //永远不能用实例化对象作为同步监视器对象，一定无效
            //synchronized (123) {  //同步监视器对象一定是引用类型
            //synchronized ("a") {  //虽然可以锁上，但a并不是合适的锁对象
            synchronized (this) {   //锁的当前shop对象，只要是同一个对象，就一定会被锁住
                System.out.println(t.getName() + "正在试衣服");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"正在结账");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}